﻿using DevExpress.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using 聚欣订单分析.Ling;
using 聚欣订单分析.PublicClass;

namespace 聚欣订单分析.Borrow
{
    public partial class BorrowData : System.Web.UI.Page
    {
        PLSQLClass pl = new PLSQLClass();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.ASPxGridView1.AddNewRow();
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            List<object> keyValues = this.ASPxGridView1.GetSelectedFieldValues("协议编号");
            if (keyValues.Count == 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert('请选择删除的数据!')</script>");
                return;
            }
            using (up_GeneralDataContext up_General = new up_GeneralDataContext())
            {
                for (int i = 0; i < keyValues.Count; i++)
                {
                    up_General.up_GeneralDeleteProc("tbl_JH", "协议编号='" + keyValues[i] + "'");
                    up_General.up_GeneralDeleteProc("tbl_JHWP", "协议编号='" + keyValues[i] + "'");
                }
            }
            ASPxGridView1.Selection.UnselectAll();
            ASPxGridView1.DataBind();
        }

        //protected void LinqServerModeDataSource1_Selecting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceSelectEventArgs e)
        //{
        //    tbl_JHDataContext JHDC = new tbl_JHDataContext();
        //    //  e.QueryableSource = from t in JHDC.tbl_JH select new { ID = t.协议编号, 借货类型 = t.借货类型, 借货日期 = t.借货日期, 借货人 = t.借货人, 长期 = t.长期, 已借天数 = (DateTime.Now.Subtract(t.借货日期.Value).Days) + 1, 物品 = t.物品, 数量 = t.数量, 用途 = t.用途, 备注 = t.备注 };
        //    //(DateTime.Now.Subtract(t.借货日期.Value).Days) + 1
        //    var a = from t in JHDC.tbl_JH select new { t.协议编号, t.借货类型, t.借货日期, t.长期, t.借货人, 借出天数 = (DateTime.Now- t.借货日期.Value).Days, t.物品, t.数量, t.用途, t.备注 };
        //    e.KeyExpression = "协议编号";
        //    e.QueryableSource = a;
        //}

        protected void ASPxGridView1_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
        {
            string strXybh = "";
            if (e.NewValues["协议编号"] == null)
            {
                e.RowError = "协议编号不能为空！";
                return;
            }
            else
            {
                strXybh = e.NewValues["协议编号"].ToString();
            }
            string strJhlx = "";
            if (e.NewValues["借货类型"] == null)
            {
                e.RowError = "借货类型不能为空！";
                return;
            }
            else
            {
                strJhlx = e.NewValues["借货类型"].ToString();
            }
            string jhqr = "";
            if (e.NewValues["借货日期"] == null)
            {
                e.RowError = "借货日期不能为空！";
                return;
            }
            else
            {
                jhqr = e.NewValues["借货日期"].ToString();
            }
            string jhr = "";
            if (e.NewValues["借货人"] == null)
            {
                e.RowError = "借货人不能为空！";
                return;
            }
            else
            {
                jhr = e.NewValues["借货人"].ToString();
            }
            string strqy = "";
            if (e.NewValues["区域"] == null)
            {
                e.RowError = "区域不能为空！";
                return;
            }
            else
            {
                strqy = e.NewValues["区域"].ToString();
            }
            string stryt = "";
            if (e.NewValues["用途"] != null)
            {
                stryt = e.NewValues["用途"].ToString();
            }
            string strbz = "";
            if (e.NewValues["备注"] != null)
            {
                strbz = e.NewValues["备注"].ToString();
            }
            
            if (!ASPxGridView1.IsNewRowEditing)
            {
                pl.SqlGetData("Update tbl_JH set 协议编号='"+ strXybh + "',长期='" + e.NewValues["长期"].ToString() + "',借货类型='" + strJhlx + "',借货日期='" + jhqr + "',借货人='" + jhr + "',区域='" + strqy + "',用途='" + stryt + "',备注='" + strbz + "' where 协议编号='" + e.OldValues["协议编号"].ToString() + "'");
                ASPxGridView1.CancelEdit();
            }
        }

        protected void detailGrid_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
        {
            if (e.NewValues["物品"] == null)
            {
                e.RowError = "物品不能为空！";
                return;
            }
            if (e.NewValues["数量"] == null)
            {
                e.RowError = "数量不能为空！";
                return;
            }
            if (e.NewValues["归还"] != null)
            {
                if (Convert.ToBoolean(e.NewValues["归还"]))
                {
                    if (e.NewValues["归还日期"] == null)
                    {
                        e.RowError = "归还日期不能为空！";
                    }
                }
            }
        }

        protected void LinqServerModeDataSource1_Inserting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceEditEventArgs e)
        {
            if (e.Values["长期"] == null)
            {
                e.Values["长期"] = false;
            }
            e.Values["归还"] = false;
        }

        protected void detailGrid_BeforePerformDataSelect(object sender, EventArgs e)
        {
            Session["CustomerID"] = (sender as ASPxGridView).GetMasterRowKeyValue();
        }

        private void JcGh()
        {
            int zs = Convert.ToInt32(pl.SqlReaderData("SELECT COUNT(*) from [tbl_JHWP] where 协议编号='" + Session["CustomerID"].ToString() + "'"));
            int ghs = Convert.ToInt32(pl.SqlReaderData("SELECT COUNT(*) from [tbl_JHWP] where 协议编号='" + Session["CustomerID"].ToString() + "' and 归还=1"));
            if (zs == ghs)
            {
                pl.SqlGetData("Update tbl_JH set 归还=1 where 协议编号='" + Session["CustomerID"].ToString() + "'");
            }
            else
            {
                pl.SqlGetData("Update tbl_JH set 归还=0 where 协议编号='" + Session["CustomerID"].ToString() + "'");
            }
        }

        protected void detailGrid_RowUpdated(object sender, DevExpress.Web.Data.ASPxDataUpdatedEventArgs e)
        {
            JcGh();
        }

        protected void detailGrid_RowInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e)
        {
            JcGh();
        }
    }
}